package com.weihui.jxc.view;

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import javax.swing.table.AbstractTableModel;

import com.weihui.jxc.common.CommonFactory;
import com.weihui.jxc.model.Customer;
import com.weihui.jxc.service.CustomerService;

import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
/**
 * 客户信息查询界面
 * @author xiaowei
 *
 */
public class CustomerInforSearchFrame extends JInternalFrame implements ActionListener{
	private JTextField searchValue;
	private JComboBox comboBox;//查询选项
    private JTable table;//表格一定要放在JScrollPane，这样数据多的时候，就可以滑动表格
    private MyTableModel tableModel;//自定义的表格模板，为了适配数据
    private CustomerService customerService;
	/**
	 * 初始化视窗
	 */
	public CustomerInforSearchFrame() {
		//支持放大缩小、可关闭
		super("客户查询", true, true, true, true);
		//获取屏幕尺寸
		Dimension screenSize =Toolkit.getDefaultToolkit().getScreenSize();
		setBounds(0,0,screenSize.width*2/3,screenSize.height*2/3);
		
		JPanel panel = new JPanel();
		getContentPane().add(panel, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("请选择查询条件：");
		panel.add(lblNewLabel);
		
		comboBox = new JComboBox();
		comboBox.setModel(new DefaultComboBoxModel(new String[] {"客户ID", "客户全称", "客户简称", "联系人员"}));
		panel.add(comboBox);
		
		searchValue = new JTextField();
		panel.add(searchValue);
		searchValue.setColumns(25);
		
		JButton searchOne = new JButton("查询");
		//给“查询”按钮绑定监听事件
		searchOne.addActionListener(this);
		panel.add(searchOne);
		
		JButton searchList = new JButton("显示全部信息");
		//给“显示全部信息”按钮绑定监听事件
		searchList.addActionListener(this);
		panel.add(searchList);
		//创建表格的列（标题栏文字）
		String[] columnNames = {"客户ID","客户全称","客户简称","客户地址","客户邮编","客户电话","客户传真","联系人员",
				"联系电话","开户银行","银行账户","客户邮箱"};
		//实例化模板
		tableModel = new MyTableModel<Customer>(columnNames, Customer.class);
		//创建表格面板
		table = new JTable(tableModel);
		//设置表格的大小
		table.setPreferredScrollableViewportSize(new Dimension(screenSize.width*2/3-60, screenSize.height/3));
		//视图高度填充
		table.setFillsViewportHeight(true);
		table.setAutoCreateRowSorter(true);
		//将表格放到滑动面板中
		JScrollPane scrollPane = new JScrollPane(table);
		getContentPane().add(scrollPane, BorderLayout.CENTER);
		//实例化service
		customerService = CommonFactory.getCustomerService();
		//更新数据
		//updateAllData(null);
	}
	
	/**
	 * 点击事件
	 */
    @Override
    public void actionPerformed(ActionEvent e) {
    	String target = e.getActionCommand();
    	switch (target) {
		case "查询":
			//搜索框非空验证
			String searchVal = searchValue.getText().trim();
			if(searchVal==null || searchVal.length()==0) {
				JOptionPane.showMessageDialog(null, "请输入搜索的值","警告",JOptionPane.WARNING_MESSAGE);
				return;
			}
			String type = comboBox.getSelectedItem().toString();//获取查询类型
			//封装查询的数据
			Customer c = new Customer();
			switch (type) {
			case "客户ID":
				c.setId(searchVal);
				break;
            case "客户全称":
				c.setCustomerName(searchVal);
				break;
            case "客户简称":
				c.setShorts(searchVal);
				break;
            case "联系人员":
				c.setContacts(searchVal);
				break;
			default:
				break;
			}
			//更新数据
			updateAllData(c);
			break;
		case "显示全部信息":
			//更新数据
			updateAllData(null);
			break;
		default:
			break;
		}
    	
    }
    /**
     * 更新表格数据
     * @param c
     */
	private void updateAllData(Customer c) {
		//查询数据库
		List<Customer> customers = customerService.findCustomers(c);
		if(customers!=null) {
			Vector<Customer> list = new Vector<Customer>();
			for (Customer customer : customers) {
				list.add(customer);
			}
			//更新数据
			tableModel.updateData(list);
			//清空输入框中的值
			searchValue.setText("");
		}
	}
}